﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using CNotebook;
namespace NotebookS2
{
    public partial class BRW_CASHRECEIPT_CLEARING : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public BRW_CASHRECEIPT_CLEARING()
        {
            InitializeComponent();
        }

        public DataTable CheckClearingItems { get; set; }
        public string ConnectionString { get; set; }

        void InitializeTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn("IsSelected", typeof(bool)));

            dt.Columns.Add(new DataColumn("DocNum", typeof(string)));
            dt.Columns.Add(new DataColumn("BankName", typeof(string)));
            dt.Columns.Add(new DataColumn("CheckNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("CheckAmount", typeof(double)));
            dt.Columns.Add(new DataColumn("CheckDate", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("CheckStatus", typeof(string)));

            this.CheckClearingItems = dt;
            
        }
        private void BRW_CASHRECEIPT_CLEARING_Load(object sender, EventArgs e)
        {
            InitializeTable();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            CashReceiptManager oManager = new CashReceiptManager();

            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();
            this.CheckClearingItems = oManager.GetCheckForClearing((DateTime)dtAsOf.EditValue);
            oManager.Close();

            gridControl1.DataSource = this.CheckClearingItems;
            gridControl1.Refresh();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to clear the selected checks?", "Clear", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                foreach (DataRow oRow in this.CheckClearingItems.Rows)
                {
                    if ((bool)oRow["IsSelected"] == true)
                    {
                        CashReceiptManager oManager = new CashReceiptManager();

                        oManager.ConnectionString = this.ConnectionString;

                        oManager.Open();

                        oManager.ClearCheck(oRow["DocNum"].ToString(), oRow["BankName"].ToString(), oRow["CheckNumber"].ToString());

                        oManager.Close();
                    }
                }

                CashReceiptManager _Manager = new CashReceiptManager();

                _Manager.ConnectionString = this.ConnectionString;

                _Manager.Open();
                this.CheckClearingItems = _Manager.GetCheckForClearing((DateTime)dtAsOf.EditValue);
                _Manager.Close();

                gridControl1.DataSource = this.CheckClearingItems;
                gridControl1.Refresh();
            }
        }
    }
}